package com.yanfan.access.service.impl;

import java.util.Arrays;
import java.util.List;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.utils.OauthUtil;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanfan.access.mapper.NjAccessRecordsMapper;
import com.yanfan.access.domain.NjAccessRecords;
import com.yanfan.access.service.INjAccessRecordsService;

/**
 * 门禁进出记录Service业务层处理
 *
 * @author genluo
 * @date 2024-10-29
 */
@Service
public class NjAccessRecordsServiceImpl extends ServiceImpl<NjAccessRecordsMapper, NjAccessRecords> implements INjAccessRecordsService {
    @Autowired
    private NjAccessRecordsMapper njAccessRecordsMapper;


    /**
     * 查询门禁进出记录列表
     *
     * @param njAccessRecords 门禁进出记录
     * @return 门禁进出记录
     */
    @Override
    public List<NjAccessRecords> queryPageList(NjAccessRecords njAccessRecords) {
        LambdaQueryWrapper<NjAccessRecords> lqw = buildQueryWrapper(njAccessRecords);
        List<NjAccessRecords> list = njAccessRecordsMapper.selectList(lqw);
        String publicKey = OauthUtil.getPublicKey();
        try {
            String accessToken = OauthUtil.accessToken(publicKey);
            list.stream().forEach(accessRecord->{
                accessRecord.setRecordImageUrl("https://192.168.254.6:443" +
                        "/evo-apigw/evo-oss/" + accessRecord.getRecordImageUrl() + "?token="
                        + accessToken);

            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
        //连表查询可用该方法，在此方法SQL语句上进行扩展
        //return njAccessRecordsMapper.queryPageList(njAccessRecords);
    }

    /**
    * 分页查询条件
    * @return
    */
    private LambdaQueryWrapper<NjAccessRecords> buildQueryWrapper(NjAccessRecords bo) {
        LambdaQueryWrapper<NjAccessRecords> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), NjAccessRecords::getDeviceName, bo.getDeviceName());
        lqw.eq(bo.getEnterOrExit() != null, NjAccessRecords::getEnterOrExit, bo.getEnterOrExit());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), NjAccessRecords::getOrgName, bo.getOrgName());
        lqw.like(StringUtils.isNotBlank(bo.getPersonName()), NjAccessRecords::getPersonName, bo.getPersonName());
        lqw.eq(StringUtils.isNotBlank(bo.getSwingTime()), NjAccessRecords::getSwingTime, bo.getSwingTime());
        lqw.orderByDesc(NjAccessRecords::getSwingTime);
        return lqw;
    }



}
